<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form
      ref="userForm"
      :model="userDetail"
      label-width="100px"
      :rules="rules"
    >
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input
              v-model="userDetail.departmentName"
              @focus="showTree = true"
            />
            <!--部门选择 -->
            <el-dep-select :visible.sync="showTree" @current-select="getDept" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userDetail.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
            <!--
              需求:
              1.已有的头像显示回显
              2.能够重新选择上传并可以获取最新的上传图片的地址
             -->
            <UploadImg :src.sync="userDetail.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="updateSave">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
export default {
  props: {
    userDetail: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      showTree: false,
      rules: {
        departmentName: { required: true, message: '部门不能为空', trigger: 'blur' },
        timeOfEntry: { required: true, message: '入职时间不能为空', trigger: 'blur' }
      }
    }
  },
  methods: {
    async updateSave () {
      this.$refs.userForm.validate(async isOk => {
        if (isOk) {
          await saveUserDetailById(this.userDetail)
          // 提示
          this.$message.success('更新成功')
        }
      })
    },
    getDept (node) {
      // 赋值
      this.userDetail.departmentName = node.name
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
